export const cloudToHttp = (str) => {
  return str
    .replace('cloud://', 'https://')
    .replace(str.split('/')[2], str.split('/')[2] + '.normal.cloudstatic.cn');
};

//压缩图片
export const compressImage = (blob, quality = 0.6) => {
  return new Promise((resolve, reject) => {
    const img = new Image();
    img.src = blob;
    img.onload = () => {
      const canvas = document.createElement('canvas');
      const ctx = canvas.getContext('2d');
      canvas.width = img.width;
      canvas.height = img.height;
      ctx.drawImage(img, 0, 0, img.width, img.height);
      canvas.toBlob((blob) => {
        const webBlobUrl = URL.createObjectURL(blob);
        resolve(webBlobUrl);
      }, 'image/webp', quality);
    };
  });
};

export function getSmallImage(url,width=200) {
  if(!url) return '../../static/images/nopic.png';
  return url + '?x-oss-process=image/resize,w_'+width;
}

export const httpToCloud = (url) => {
  return url
    .replace('https://', 'cloud://')
    .replace('.normal.cloudstatic.cn', '');
};

